package com.isoft.airportsystem.controller;

import com.isoft.airportsystem.entity.flight.BaggageInfo;
import com.isoft.airportsystem.service.flight.BaggageService;
import com.isoft.airportsystem.util.PageResult;
import com.isoft.airportsystem.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/baggage")
@CrossOrigin(origins = {"http://localhost:8080", "http://localhost:3000", "http://localhost:5173"}, allowCredentials = "true")
public class BaggageController {

    @Autowired
    private BaggageService baggageService;

    @GetMapping("/flight/{flightId}")
    public Result<List<BaggageInfo>> getByFlightId(@PathVariable String flightId) {
        return Result.success(baggageService.getByFlightId(flightId));
    }

    @GetMapping("/tag/{tag}")
    public Result<BaggageInfo> getByTag(@PathVariable String tag) {
        return Result.success(baggageService.getByTag(tag));
    }


    @GetMapping("/belt/{beltNumber}")
    public Result<List<BaggageInfo>> getByBeltNumber(@PathVariable String beltNumber) {
        return Result.success(baggageService.getByBeltNumber(beltNumber));
    }

    @GetMapping("/page")
    public Result<PageResult<BaggageInfo>> queryBaggagePage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            BaggageInfo baggage) {
        return Result.success(baggageService.queryBaggagePage(pageNum, pageSize, baggage));
    }

    @PostMapping
    public Result<Void> addBaggage(@RequestBody BaggageInfo baggage) {
        boolean success = baggageService.addBaggage(baggage);
        return success ? Result.success() : Result.fail("新增失败");
    }

    @PutMapping
    public Result<Void> updateBaggage(@RequestBody BaggageInfo baggage) {
        boolean success = baggageService.updateBaggage(baggage);
        return success ? Result.success() : Result.fail("修改失败");
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteBaggage(@PathVariable String id) {
        boolean success = baggageService.deleteBaggage(id);
        return success ? Result.success() : Result.fail("删除失败");
    }
}
